package com.ruoyi.web.controller.location;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SeqNumberUtils;
import com.ruoyi.shelf.domain.Shelf;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.location.domain.Location;
import com.ruoyi.location.service.ILocationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 货位管理Controller
 * 
 * @author lh
 * @date 2024-10-28
 */
@RestController
@RequestMapping("/location/location")
public class LocationController extends BaseController
{
    @Autowired
    private ILocationService locationService;

    /**
     * 查询货位管理列表
     */
    @PreAuthorize("@ss.hasPermi('location:location:list')")
    @GetMapping("/list")
    public TableDataInfo list(Location location)
    {
        startPage();
        List<Location> list = locationService.selectLocationList(location);
        return getDataTable(list);
    }

    /**
     * 导出货位管理列表
     */
    @PreAuthorize("@ss.hasPermi('location:location:export')")
    @Log(title = "货位管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Location location)
    {
        List<Location> list = locationService.selectLocationList(location);
        ExcelUtil<Location> util = new ExcelUtil<Location>(Location.class);
        util.exportExcel(response, list, "货位管理数据");
    }

    /**
     * 获取货位管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('location:location:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(locationService.selectLocationById(id));
    }

    /**
     * 新增货位管理
     */
    @PreAuthorize("@ss.hasPermi('location:location:add')")
    @Log(title = "货位管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Location location)
    {
        location.setLocationNo(SeqNumberUtils.getHWNum());
        return toAjax(locationService.insertLocation(location));
    }

    @PreAuthorize("@ss.hasPermi('location:location:addAll')")
    @PostMapping("/addAll")
    public AjaxResult add(@RequestBody Map<String, Object> params)
    {
        System.out.println(params);
        Integer shelfIdInt = (Integer) params.get("shelfId");
        Long shelfId = shelfIdInt != null ? shelfIdInt.longValue() : null;
        Integer locationCount = Integer.valueOf((String) params.get("locationCount"));
//        Integer shelfId = Integer.valueOf((String) );
//        Integer locationCount = Integer.valueOf((String) params.get("locationCount"));
        System.out.println(shelfId);
        System.out.println(locationCount);
        ArrayList<Location> list = new ArrayList<>();
        for (int i = 0; i < locationCount; i++) {
            Location location1 = new Location();
            location1.setStatus("0");
            location1.setShelfId(shelfId);
            location1.setLocationNo(SeqNumberUtils.getHWNum());
            list.add(location1);
        }
        System.out.println("list"+list);
        return toAjax(locationService.insertAllLocation(list));
    }
    /**
     * 修改货位管理
     */
    @PreAuthorize("@ss.hasPermi('location:location:edit')")
    @Log(title = "货位管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Location location)
    {
        return toAjax(locationService.updateLocation(location));
    }

    /**
     * 删除货位管理
     */
    @PreAuthorize("@ss.hasPermi('location:location:remove')")
    @Log(title = "货位管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(locationService.deleteLocationByIds(ids));
    }

    /**
     * 获取该货架下的所有货位
     */
    @PreAuthorize("@ss.hasPermi('location:location:selectByPid')")
    @GetMapping( "selectByPid")
    public TableDataInfo selectByPid(Long id,String locationNo,Integer status)
    {
        startPage();
        List<Location> list = locationService.selectByPid(id,locationNo,status);
        return getDataTable(list);
    }

    /**
     * 查询没有占用的货位
     */
    @GetMapping("select")
    public TableDataInfo select(Location location){
        startPage();
        List<Location> list = locationService.selectLocation(location);
        return getDataTable(list);
    }

    /**
     * 查询修改的货位
     */
    @GetMapping("selectup")
    public TableDataInfo selectxg(Location location){
        startPage();
        List<Location> list = locationService.select(location);
        return getDataTable(list);
    }
}
